<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            var lowestCommonAncestor = function (root, p, q) {
                if (!root) {
                    return null
                }
                if (root == p || root == q) {
                    return root
                }
                let left = lowestCommonAncestor(root.left, p, q)
                let right = lowestCommonAncestor(root.right, p, q)
                if (left && right) {
                    return root
                }
                if (left) {
                    return left
                }
                if (right) {
                    return right
                }
            }
        </script>
    </body>
</html>
